<html>
<head>
    <meta charset="utf-8">
    <script src="../js/jquery.min.js"></script>
</head>
<body>
    <div>
        <h3>创建商品</h3>
        <div>
            <label>商品名称</label>
            <div>
                <input type="text" placeholder="商品名称" name="title" id="title"/>
            </div>
        </div>
        <div>
            <label>商品描述</label>
            <div>
                <input type="text" placeholder="商品描述" name="descp" id="descp"/>
            </div>
        </div>
        <div>
            <label>价格</label>
            <div>
                <input type="text" placeholder="价格" name="price" id="price"/>
            </div>
        </div>
        <div>
            <label>图片</label>
            <div>
                <input type="text" placeholder="图片" name="imgUrl" id="imgUrl"/>
            </div>
        </div>
        <div>
            <label>库存</label>
            <div>
                <input type="text" placeholder="库存" name="stock" id="stock"/>
            </div>
        </div>
    </div>
    <div>
        <button id="create" type="submit">
           提交创建
        </button>
    </div>
</body>
<script>
    jQuery(document).ready(function () {
        $("#create").on("click",function () {//
            var title = $("#title").val();
            var descp = $("#descp").val();
            var price = $("#price").val();
            var imgUrl = $("#imgUrl").val();
            var stock = $("#stock").val();
            if(title == null || title == ""){
                alert("商品名称不能为空");
                return false;
            }
            if(descp == null || descp == ""){
                alert("商品描述不能为空");
                return false;
            }
            if(price == null || price == ""){
                alert("商品价格不能为空");
                return false;
            }
            if(imgUrl == null || imgUrl == ""){
                alert("商品图片url不能为空");
                return false;
            }
            if(stock == null || stock == ""){
                alert("商品库存不能为空");
                return false;
            }
            $.ajax({
                type:"POST",
                contentType:"application/x-www-form-urlencoded",
                url:"http://localhost:8090/item/create",
                data:{
                    "title":title,
                    "descp":descp,
                    "price":price,
                    "imgUrl":imgUrl,
                    "stock":stock
                },
                xhrFields:{withCredentials:true},
                success:function (data) {
                    if(data.status == "success"){
                        alert("创建商品成功！");
                        window.location.href = "listItem.html";
                    }else{
                        alert("创建商品失败，原因："+data.data.errMsg);
                    }
                },
                error:function (data) {
                    alert("创建商品失败，原因："+data.responseText);
                }
            });
            return false;
        });
    });
</script>
</html>